﻿using GrainManage.Data.Model;
using GrainManage.Data.Model.Filter;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Service.Util;
using System;
using System.Collections.Generic;
using System.Data;

namespace GrainManage.Data.Service
{
    public class PriceService : BaseService, GrainManage.Data.Service.Interfaces.IPriceService
    {

        public PriceService(GrainManage.Data.Access.IAccess proc) : base(proc) { }

        public PriceTable Get(int priceID, string creator)
        {
            DataSet grain = Proc.ExecuteQuery(ProcConst.Price_GetByID, priceID, creator);
            return ConvertToModel<PriceTable>(GetFirstRow(grain));
        }

        public PriceTable GetRecentPriceByGrain(string priceType, string grain, string creator)
        {
            DataSet ds = Proc.ExecuteQuery(ProcConst.Price_GetRecentByGrain, priceType, grain, creator);
            return ConvertToModel<PriceTable>(GetFirstRow(ds));
        }

        public List<PriceTable> SearchRecentPrice(PriceFilter filter, IPageable page, ICountable records, string creator)
        {
            DataSet grain = Proc.ExecuteQuery(ProcConst.Price_SearchRecent, filter.PriceType, filter.Grain, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<PriceTable>(grain, records);
        }

        public List<PriceTable> SearchPrice(PriceFilter filter, IPageable page, ICountable records, string creator)
        {
            DataSet grain = Proc.ExecuteQuery(ProcConst.Price_Search, filter.PriceType, filter.Grain,
                filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<PriceTable>(grain, records);
        }

        public int Insert(PriceTable price)
        {
            Initializor.Initialize(price, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Price_Insert, price.Grain, price.UnitPrice, price.PriceType, price.Remarks, price.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public int Update(PriceTable price)
        {
            Initializor.Initialize(price, string.Empty);
            return Proc.Execute(ProcConst.Price_UpdateByID, price.PriceID, price.Grain, price.UnitPrice, price.PriceType, price.Remarks, price.Creator);
        }

        public int Delete(int priceID, string creator)
        {
            return Proc.Execute(ProcConst.Price_DeleteByID, priceID, creator);
        }

        public List<int> Insert(List<PriceTable> prices)
        {
            var priceIDs = new List<int>();
            foreach (var price in prices)
            {
                priceIDs.Add(Insert(price));
            }
            return priceIDs;
        }

        public List<int> Update(List<PriceTable> prices)
        {
            var invalidPrices = new List<int>();
            foreach (var price in prices)
            {
                if (Update(price) == 0)
                {
                    invalidPrices.Add(price.PriceID);
                }
            }
            return invalidPrices;
        }

        public List<int> Delete(List<int> prices, string creator)
        {
            var invalidPrices = new List<int>();
            foreach (var price in prices)
            {
                if (Delete(price, creator) == 0)
                {
                    invalidPrices.Add(price);
                }
            }
            return invalidPrices;
        }
    }
}
